package utils

import (
	"gitee.com/showurl/push-rtmp/config"
	"gitee.com/showurl/push-rtmp/models"
	"gorm.io/gorm"
	"os/exec"
	"strings"
	"time"
)

func Jump(name string, db *gorm.DB) {
	var pipeis []string
	for _, mp4Path := range config.Config.Playlist {
		if strings.Contains(mp4Path, name) {
			pipeis = append(pipeis, mp4Path)
		}
	}
	if len(pipeis) == 0 {
		DingTalkSendMsg("没匹配到，当前列表：", config.Config.Playlist...)
		return
	}
	if len(pipeis) == 1 {
		// 直接去跳转
		jump(pipeis[0], db)
		return
	}
	// 匹配到太多了
	DingTalkSendMsg("匹配多个视频文件", pipeis...)
	return
}
func jump(mp4Path string, db *gorm.DB) {
	duration, err := GetMP4Duration(mp4Path)
	if err != nil {
		DingTalkSendMsg("获取视频长度失败", err.Error())
		return
	}
	m := &models.ModelCurrentInfo{
		Mp4Path:       mp4Path,
		Seconds:       int(duration),
		StartPlayTime: time.Now(),
		HealthSeconds: 0,
	}
	err = db.Model(m).Create(m).Error
	if err != nil {
		DingTalkSendMsg("插入失败", err.Error())
		return
	}
	err = Command(exec.Command("killall", "-9", "ffmpeg"))
	if err != nil {
		DingTalkSendMsg("killall失败", err.Error())
		return
	}
}
